﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;
using TreasureJunk;

public partial class Pages_Reservation_ReservationAdd : Page
{
    #region Fields

    public DateTime DepartureDate
    {
        get
        {
            if (Request["DepartureDate"] != null)
                return DateTime.Parse(Request["DepartureDate"], CultureInfo.InvariantCulture);
            throw new NullReferenceException();
        }
    }

    /// <summary>
    ///     BoardDate
    /// </summary>
    public DateTime BoardDate
    {
        get
        {
            if (Request["BoardDate"] != null) return DateTime.Parse(Request["BoardDate"], CultureInfo.InvariantCulture);
            throw new NullReferenceException();
        }
    }

    public int NoOfNights
    {
        get
        {
            if (Request["NoOfNights"] != null) return int.Parse(Request["NoOfNights"]);
            throw new NullReferenceException();
        }
    }

    public int NoOfCabins
    {
        get
        {
            if (ViewState["NoOfCabins"] != null) return Convert.ToInt32(ViewState["NoOfCabins"]);
            if (Request["NoOfCabins"] != null) return int.Parse(Request["NoOfCabins"]);
            throw new NullReferenceException();
        }
        set { ViewState["NoOfCabins"] = value; }
    }

    public string BoatID
    {
        get
        {
            if (Request["BoatID"] != null) return Request["BoatID"];
            throw new NullReferenceException();
        }
    }

    public bool IsCharter
    {
        get
        {
            if (Request["IsCharter"] != null) return bool.Parse(Request["IsCharter"]);
            throw new NullReferenceException();
        }
    }

    #endregion

    #region Events

    /// <summary>
    ///     Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Load all to comboBoxes
            LoadCombo();

            BoatEntity boat = BoatDAO.CreateInstant().GetBoat(BoatID);

            if (boat != null)
            {
                if (IsCharter) NoOfCabins = boat.NumOfCabins;

                LiteralBoat.Text = boat.BoatName;
                LiteralDepartureDate.Text = DepartureDate.ToShortDate();
                LiteralCheckInData.Text = BoardDate.ToShortDate();
                LiteralDataNumber.Text = NoOfNights.ToString(CultureInfo.InvariantCulture);
                LiteralCabinNumber.Text = NoOfCabins.ToString(CultureInfo.InvariantCulture);

                rpt.DataSource = CabinDAO.CreateInstant()
                    .GetCabinBoat(IsCharter, BoatID, BoardDate, NoOfNights, NoOfCabins);
                rpt.DataBind();
            }
        }
    }

    /// <summary>
    ///     Load combobox
    /// </summary>
    private void LoadCombo()
    {
        // Load country list
        ddlCountry.DataSource = DropDownListHelper.Countries;
        ddlCountry.DataTextField = "Value";
        ddlCountry.DataValueField = "ParamID";
        ddlCountry.DataBind();

        // Load all currency
        ddlReservationCurrency.Items.AddRange(DropDownListHelper.Currencies);

        // Get exchange rate
        new Get().GetExchange(define.DefaultCurrency, DateTime.Now.ToShortDate());
    }

    /// <summary>
    ///     Bound reservation details
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var ddlBedrooms = e.Item.Find<DropDownList>("ddlBedrooms");
            var ddlNumberClient = e.Item.Find<DropDownList>("ddlNumberClient");
            var ddlBlockDates = e.Item.Find<DropDownList>("ddlBlockDates");

            var hdfCabinID = e.Item.Find<TextBox>("hdfCabinID");
            var txtCabinNumber = e.Item.Find<TextBox>("txtCabinNumber");
            var txtPrice = e.Item.Find<TextBox>("txtPrice");
            var txtRepresentative = e.Item.Find<TextBox>("txtRepresentative");
            var txtNoteDetail = e.Item.Find<TextBox>("txtNote");

            txtRepresentative.Text = txtBookingCode.Text;
            ddlBlockDates.Text = NoOfNights.ToString(CultureInfo.InvariantCulture);

            ddlBlockDates.Attributes.AddClassAndIndex("blockdate blockdate-{0}", e.Item.ItemIndex);
            ddlNumberClient.Attributes.AddClassAndIndex("numberclient numberclient-{0}", e.Item.ItemIndex);
            ddlBedrooms.Attributes.AddClassAndIndex("typeofcabin typeofcabin-{0}", e.Item.ItemIndex);

            hdfCabinID.Attributes.AddClassAndIndex("cabinid cabinid-{0}", e.Item.ItemIndex);

            txtRepresentative.Attributes.AddClassAndIndex("representative representative-{0}", e.Item.ItemIndex);
            txtCabinNumber.Attributes.AddClassAndIndex("cabinnumber cabinnumber-{0}", e.Item.ItemIndex);
            txtPrice.Attributes.AddClassAndIndex("auto price cabinprice-{0}", e.Item.ItemIndex);
            txtNoteDetail.Attributes.AddClassAndIndex("notedetail notedetail-{0}", e.Item.ItemIndex);

            if (IsCharter)
            {
                txtPrice.Text = @"0";
            }
        }
    }

    #endregion
}